package com.winter.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.AreaCodeMapper;
import com.winter.mapper.CompanyMapper;
import com.winter.mapper.CompanyPositionMapper;
import com.winter.mapper.CompanyUserMapper;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.IeliteRelainfoMapper;
import com.winter.mapper.PeoplejobsMapper;
import com.winter.model.AreaCode;
import com.winter.model.Company;
import com.winter.model.CompanyPosition;
import com.winter.model.CompanyUser;
import com.winter.model.DictEntry;
import com.winter.model.vo.check.CheckParam;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import cn.jiguang.common.utils.StringUtils;

@Service
public class CompanyService {

    @Autowired
    public CompanyMapper companyMapper;

    @Autowired
    public CompanyUserMapper companyUserMapper;

    @Autowired
    public CompanyPositionMapper companyPositionMapper;

    @Autowired
    public PeoplejobsMapper peoplejobsMapper;

    @Autowired
    public IeliteRelainfoMapper ieliteRelainfoMapper;

    @Autowired
    private DictEntryMapper dictEntryMapper;

    @Autowired
    private AreaCodeMapper areaCodeMapper;


    /**
     * 查看评论列表(分页)
     */
    public Result queryPage(Integer pageNumber, Integer pageSize, Date timeStart, Date timeEnd, String companyIndustry) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        String companyreleaseId = "companyreleaseId";
        String orderBy = "c2.publish_time desc";
        PageHelper.startPage(pageNumber, pageSize, false);

        List<Company> finCommentInfo = companyMapper.queryPage(companyreleaseId, orderBy, timeStart, timeEnd, companyIndustry);
        PageInfo<Company> pageInfo = new PageInfo<>(finCommentInfo);
        return Result.success(pageInfo);
    }

    /**
     * 添加公司  添加用户公司表
     */
    public Result insert(Company record) {
        if (Strings.isNullOrEmpty(record.getUserNo())) {
            return Result.failure(ResultCode.FAILURE, "用户帐号不能为空");
        }
        // 校验参数
        String checkCompany = CheckParam.checkCompany(record);
        if (checkCompany != null) {
            return Result.failure(ResultCode.TRUSTED_USER_EXIST, checkCompany);
        }
        if (record.getCreatetime() == null) {
            record.setCreatetime(new Date());
        }
        record.setIsvalid("1");
        record.setCreateUser(record.getUserNo());
        record.setUpdateUser(record.getUserNo());
        record.setUpdatetime(new Date());
        int i = companyMapper.insertSelective(record);
        if (i <= 0) {
            return Result.failure(ResultCode.FAILURE, "添加公司失败");
        }
        CompanyUser companyUser = new CompanyUser();
        companyUser.setCreatetime(new Date());
        if (!Strings.isNullOrEmpty(record.getCreateUser())) {
            companyUser.setCreateuser(record.getCreateUser());
        }
        if (record.getUserNo() != null) {
            companyUser.setUserno(record.getUserNo());
        }
        if (record.getId() != null) {
            companyUser.setOrgid(record.getId());
        }
        companyUser.setUpdateuser(record.getUserNo());
        companyUser.setUpdatetime(new Date());
        companyUser.setIsvalid(record.getIsvalid());
        companyUser.setId(companyUserMapper.queryByKeyId());
        int j = companyUserMapper.insert(companyUser);
        if (j <= 0) {
            return Result.failure(ResultCode.FAILURE, "添加用户公司表失败");
        }
        return Result.success();
    }

    /**
     * 根据公司id删除
     */
    public Result delete(Long id) {
        if (id == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "公司Id不能为空!");
        }
        int i = companyMapper.deleteByPrimaryKey(id);
        if (i <= 0) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "删除失败!");
        }
        int j = companyUserMapper.deleteByPrimaryKey(id);
        if (j <= 0) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "删除失败!");
        }
        List<CompanyPosition> list = companyPositionMapper.selectByPrimaryKey(id, null, null, null);
        if (list.size() > 0) {
            int k = companyPositionMapper.deleteByPrimaryKey(id);
            if (k <= 0) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "删除失败!");
            }
            for (int k2 = 0; k2 < list.size(); k2++) {
                peoplejobsMapper.deleteByPrimaryKey(list.get(k2)
                                                        .getId());
            }
        }
        return Result.success();
    }

    /**
     * 根据公司id修改
     */
    public Result update(Company record) {
        if (record.getId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "公司Id不能为空!");
        }
        if (record.getUpdateUser() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "修改人不能为空!");
        }
        record.setUpdateUser(record.getUpdateUser());
        record.setUpdatetime(new Date());
        int i = companyMapper.updateByPrimaryKeySelective(record);
        if (i <= 0) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "修改失败");
        }
        return Result.success();
    }

    /*
     * 公司圈列表
     */
    public Result icompanyCardList(Integer pageSize, Integer pageNumber, String comClass, String comSubClass, String userNo, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        String companyIndustry = null;
        if (!Strings.isNullOrEmpty(key)) {
        	 companyIndustry = dictEntryMapper.queryDictNameLike(key);
		}
        String orderBy = "createtime desc";
        List<Company> ic1 = companyMapper.icompanyCardList(orderBy, comClass, comSubClass, key,companyIndustry);
        PageHelper.startPage(pageNumber, pageSize, false);
        List<Company> ic = companyMapper.icompanyCardList(orderBy, comClass, comSubClass, key, companyIndustry);
        for (Company ui : ic) {
            if (StringUtils.isNotEmpty(userNo)) {
                int i = ieliteRelainfoMapper.getIeliteIsColln(ui.getId(), userNo);
                if (i > 0) {
                    ui.setIsCollection("1");
                } else {
                    ui.setIsCollection("0");
                }
            } else {
                ui.setIsCollection("0");
            }
        }
        PageInfo<Company> pageInfo = new PageInfo<>(ic);
        pageInfo.setTotal(ic1.size());
        return Result.success(pageInfo);
    }

    /*
     * 通过id查询公司圈信息
     */
    public Result getIcomCard(Long id, String userNo) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户未登录,请登录");
        }
        Company ic = companyMapper.selectByPrimaryKey(id);
        if (null != ic) {
            int i = ieliteRelainfoMapper.getIeliteIsColln(ic.getId(), userNo);
            if (i > 0) {
                ic.setIsCollection("1");
            } else {
                ic.setIsCollection("0");
            }
            int a = companyPositionMapper.companyCount(ic.getId());
            //String sum=companyMapper.getCompanyId(ic.getCreateUser());
            ic.setFbPanyPostionCount(a);
            getTypeName(ic);
        }

        return Result.success(ic);
    }

    public void getTypeName(Company company) {
        DictEntry dictEntry = dictEntryMapper.get("DC_INDU_CLASSIFY", company.getCompanyIndustry());
        if (dictEntry != null) {
            company.setCompanyIndustry(dictEntry.getDictname());
        }
        DictEntry dictEntry1 = dictEntryMapper.get("DC_COM_CLASSIFY", company.getCompanyNature());
        if (dictEntry1 != null) {
            company.setCompanyNature(dictEntry1.getDictname());
        }
        DictEntry dictEntry2 = dictEntryMapper.get("DC_COMPANY_SCALE", company.getCompanyScale());
        if (dictEntry2 != null) {
            company.setCompanyScale(dictEntry2.getDictname());
        }
        if (company.getComProId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComProId());
            if (areaCode != null) {
                company.setComProName(areaCode.getChiShortName());
            }
        }
        if (company.getComCityId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComCityId());
            if (areaCode != null) {
                company.setComCityName(areaCode.getChiShortName());
            }
        }
    }

}
